package cn.edu.scau.cmi.chenyuanying.abstractFactoryMethod;

import java.net.URL;
import java.util.ResourceBundle;

import cn.edu.scau.cmi.chenyuanying.simpleFactoryMethod.Meat;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextArea;

public class AbstractFactoryMethodController implements Initializable {
	@FXML Button showButton;
	@FXML ChoiceBox<String> chooseTypeBox;
	@FXML ChoiceBox<String> chooseBrandBox;
	@FXML TextArea showTextArea;
	private String nowMeat;
	private String nowBrand;
	private final String invalid = "Please choose";
	private final String[] chooseType = {invalid, "Pork", "Beef", "Chicken"};
	private final String[] chooseBrand = {invalid, "Wins", "Aonong", "Tianbang"};

	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		nowBrand = nowMeat = invalid;
		ObservableList<String> brandList = FXCollections.observableArrayList(chooseBrand);
		chooseBrandBox.setItems(brandList);
		chooseBrandBox.getSelectionModel().selectedIndexProperty().addListener(
				new ChangeListener<Number>() {
					@Override
					public void changed(ObservableValue<? extends Number> observable, Number oldValue,
							Number newValue) {
						nowBrand = chooseBrand[newValue.intValue()];
					}
				});
		chooseBrandBox.getSelectionModel().selectFirst();
		ObservableList<String> typeList = FXCollections.observableArrayList(chooseType);
		chooseTypeBox.setItems(typeList);
		chooseTypeBox.getSelectionModel().selectedIndexProperty().addListener(
				new ChangeListener<Number>() {
					@Override
					public void changed(ObservableValue<? extends Number> observable, Number oldValue,
							Number newValue) {
						nowMeat = chooseType[newValue.intValue()];
					}
				});
		chooseTypeBox.getSelectionModel().selectFirst();
	}
	
	public void showMeat(ActionEvent e) {
		if(nowBrand.compareTo(invalid) == 0 || nowMeat.compareTo(invalid) == 0) {
			showTextArea.setText("Please Choose");
			return;
		}
		Brand factory = Brand.getFactory(nowBrand);
		Meat instance = null;
		if(nowMeat.compareTo("Pork") == 0) instance = factory.getPorkObj();
		else if(nowMeat.compareTo("Beef") == 0) instance = factory.getBeefObj();
		else instance = factory.getChickenObj();
		showTextArea.setText(
				"brand:" + factory.getClass().getSimpleName() + "\n" + 
				instance.toString());
	}

}
